$("#loginUser_Pwd").click(function() {
	var userId = $("#userId").val();
	var pwd = $("#pwd").val();
	if (userId.replace(/(^\s*)|(\s*$)/g, '') == null || userId.replace(/(^\s*)|(\s*$)/g, '') == "") {
		$('#wrong_tips').html("用户名不可为空或您没有输入有效的字符！");
		$("#wrong_tips").css("display", "inline");

		return;
	}
	if (/[^0-9]/.test(userId)) {
		$('#wrong_tips').html("用户名值不可为非数字字符！");
		$("#wrong_tips").css("display", "inline");
		return;
	}
	if (pwd.replace(/(^\s*)|(\s*$)/g, '') == null || pwd.replace(/(^\s*)|(\s*$)/g, '') == "") {
		$('#wrong_tips').html("pwd不可为空或您没有输入有效的字符！");
		$("#wrong_tips").css("display", "inline");
		return;
	}
	if (/[^0-9]/.test(pwd)) {
		$('#wrong_tips').html("pwd值不可为非数字字符！");
		$("#wrong_tips").css("display", "inline");
		return;
	}
	$("#loginUser_Pwd").attr("disabled", true);
	console.log($("#loginUser_Pwd").html());
	$("#loginUser_Pwd").html("请稍候<i id='loginUser_PwdiCon'></i>");
	$("#loginUser_PwdiCon").addClass("fa fa-refresh fa-spin fa-fw");
	$.post("../LoginServlet", {
		"userId": userId,
		"pwd": pwd,
		"Type": "login"
	}, function(ret) {
		$("#loginUser_Pwd").attr("disabled", false);
		$("#loginUser_Pwd").html("登录");
		if (ret == "null") {
			$("#wrong_tips").css("display", "inline");
			$("#wrong_tips").html("抱歉，您的用户名或密码错误，请重试！");
		} else {
			swal({
				title: "登陆成功！",
				text: "尊敬的" + ret + userId + "欢迎您使用银行信贷系统！\n该对话框将在两秒后自动为您跳转！",
				showConfirmButton: false,
				type: "success",
			});

			if (ret == "Customer") {
				setTimeout("$(location).attr('href', 'Customer/CustomerMain.html')", 2000);
			} else if (ret == "Manager") {
				setTimeout("$(location).attr('href', 'Manager/ManagerMain.html')", 2000);
			} else {
				setTimeout("$(location).attr('href', 'Admin/AdminMain.html')", 2000);
			}

		}

	});
});

//明文显示密码
$("body").on("click", "#showPwd", function() {
	if ($(this).prop('checked') == true) {
		$("#pwdRegi").attr("type", "text");
		$("#qrpwdRegi").attr("type", "text");
	} else {
		$("#pwdRegi").attr("type", "password");
		$("#qrpwdRegi").attr("type", "password");
	}
});
//检查注册密码的合法性
$.fn.checkTwoPwd = function() {
	$("#pwdRegiTips").html("");
	var qrpwd = $("#qrpwdRegi").val();
	var pwd = $("#pwdRegi").val();
	var flag = false;
	if (pwd.replace(/(^\s*)|(\s*$)/g, '') == null || pwd.replace(/(^\s*)|(\s*$)/g, '') == "") {
		$('#pwdRegiTips').html("<i class='fa fa-remove'></i>密码不可为空或您没有输入有效的字符！");
		$('#pwdRegi').focus();
		return flag;
	}
	if (qrpwd.replace(/(^\s*)|(\s*$)/g, '') == null || qrpwd.replace(/(^\s*)|(\s*$)/g, '') == "") {
		$('#pwdRegiTips').html("<i class='fa fa-remove'></i>确认密码不可为空或您没有输入有效的字符！");
		$('#qrpwdRegi').focus();
		return flag;
	}
	if (pwd.length < 6 || qrpwd.length < 6) {
		$('#pwdRegiTips').html("<i class='fa fa-remove'></i>密码或确认密码长度小于6，请检查！");
		$('#qrpwdRegi').focus();
		$('#pwdRegi').focus();
		return flag;
	}
	if (pwd != qrpwd) {
		$('#pwdRegiTips').html("<i class='fa fa-remove'></i>两次输入的密码不一致，请检查！");
		$('#qrpwdRegi').focus();
		$('#pwdRegi').focus();
		return flag;
	}

	flag = true;
	return flag;

}
//提交密码
$("body").on("click", "#submitPWD", function() {
	if ($().checkTwoPwd()) {
		$("#submitPWD").html("<i class='fa fa-refresh fa-spin fa-fw'></i>请稍候，正在与服务器通讯...");
		var tttype = "regiSetPWD";
		var peoId = $("#peoIdInput").val();//注册的员工编号
		if($("#forgetSetPwd").val()=="true"){
			tttype = "forgetSetPWD";
			peoId = $("#userFindPwdIdRight").val();//找回密码的用户名
		}
		$.post("../LoginServlet", {
			"peoId": peoId,
			"pwd": $('#qrpwdRegi').val(),
			"Type": tttype
		}, function(ret) {
			if (ret.split(" ")[0] == "Yes") {
				$("#successSetPwd").html("<h5 style='color:#1AA034; '><i class='fa fa-check'></i>" + ret.split(" ")[1] +
					"</h5>");
				$("#submitPWD").html("确认");
				$("#pwdRegi").attr("disabled", true);
				$("#qrpwdRegi").attr("disabled", true);
				$("#submitPWD").css("display", "none");
				$("#exitToLogin").css("display", "inline");
			} else {
				$("#pwdRegiTips").html(ret.split(" ")[1]);
				$("#submitPWD").html("确认");

				$("#exitToLogin").attr("display", "inline");
			}
		});
	}
});
//关闭输入验证码框的按钮
$("body").on("click", "#closeCode", function() {
	$('#emailCodeModal').modal('hide');
	$('#exitTipsModal').modal('show');
});

//返回输入验证码模态框的按钮
$("body").on("click", "#goBack", function() {
	$('#exitTipsModal').modal('hide');
	$('#emailCodeModal').modal('show');

})
//验证验证码合法性
$.fn.checkVerifyCode = function(code) {
	if (code.replace(/(^\s*)|(\s*$)/g, '') == null || code.replace(/(^\s*)|(\s*$)/g, '') == "") {
		return "<i class='fa fa-remove'></i>验证码不可为空或您没有输入有效的字符！";
	}
	if (/[^0-9]/.test(code)) {
		return "<i class='fa fa-remove'></i>验证码不可为非数字字符！";
	}
	if (code.length != 6) {
		return "<i class='fa fa-remove'></i>验证码长度不正确，请核对！";
	}
	return null;
}
//验证邮箱合法性
$.fn.checkEmailInLaw = function(email) {
	if (email.replace(/(^\s*)|(\s*$)/g, '') == null || email.replace(/(^\s*)|(\s*$)/g, '') == "") {
		return "邮箱不可为空或您没有输入有效的字符！";
	}
	if (!/^([a-zA-Z]|[0-9])(\w|\-)+@[a-zA-Z0-9]+\.([a-zA-Z]{2,4})$/.test(email)) {
		return "email值格式不正确！";
	}
	return null;
}
//验证Id合法性
$.fn.checkIdInLaw = function(id) {
	if (id.replace(/(^\s*)|(\s*$)/g, '') == null || id.replace(/(^\s*)|(\s*$)/g, '') == "") {
		return "用户编号不可为空或您没有输入有效的字符！";
	}
	if (/[^0-9]/.test(id)) {
		return "用户编号不可为非数字字符！";
	}
	return null;
}
//提交验证码
$("body").on("click", "#submitCode", function() {
	$('#codeTipsReturn').html("");
	var code = $("#emailCode").val();
	var rere = $().checkVerifyCode(code)
	if (rere != null) {
		$("#emailCode").focus();
		$('#codeTipsReturn').html(rere);
		return;
	};
	var peoId = $("#peoIdInput").val();
	if($("#forgetSetPwd").val()=="true"){//如果是找回密码选项
		peoId = $("#peoIdRight").val();
	}
	console.log("email"+peoId)
	$.post("../EmailServlet", {
		"peoId": peoId,
		"code": code,
		"Type": "verifyCode"
	}, function(ret) {
		alert(ret)
		if (ret == "Yes") {
			$("#emailCodeModal").modal('hide');
			$('#enterPWDModal').modal('show');
		} else {
			$('#codeTipsReturn').html("<i class='fa fa-remove'></i>" + ret);
		}
	});

})

$("#test").click(function() {
	Swal.fire({
		title: '领取你的寻龙装备！',
		input: 'select',
		html: '<input id="swal-input1" class="swal2-input">' +
			'<input id="swal-input2" class="swal2-input">',
		inputAttributes: {
			autocapitalize: 'off'
		},
		showCancelButton: true,
		confirmButtonText: 'Look up',
		showLoaderOnConfirm: true,
		preConfirm: (login) => {
			return fetch(`//api.github.com/users/${login}`)
				.then(response => {
					if (!response.ok) {
						throw new Error(response.statusText)
					}
					return response.json()
				})
				.catch(error => {
					Swal.showValidationMessage(
						`Request failed: ${error}`
					)
				})
		},
		allowOutsideClick: () => !Swal.isLoading()
	}).then((result) => {
		if (result.value) {
			Swal.fire({
				title: `${result.value.login}'s avatar`,
				imageUrl: result.value.avatar_url
			})
		}
	})
})
//打开找密码输入框
$("#findPWD").click(function() {
	$("#userFindPwd").modal('show');
});
//验证用户名是否存在
$("body").on("click", "#inputuserId_findPWD", function() {
	var userId = $("#useridInputToFind").val();
	var rere = $().checkIdInLaw(userId);
	if (rere != null) {
		$("#wrong_tips_findPWD").html("<i class='fa fa-remove'>" + rere);
		return;
	}
	$("#wrong_tips_findPWD").html("");
	$(this).css("display", "none");
	$("#waitingtips_findPWD").css("display", "inline");
	$.post("../LoginServlet", {
		"userId": userId,
		"Type": "useridCheck"
	}, function(ret) {
		// console.log($(this));
		$("#inputuserId_findPWD").css("display", "inline");
		$("#waitingtips_findPWD").css("display", "none");
		if (ret != userId) {
			$("#wrong_tips_findPWD").html("<i class='fa fa-remove'>" + ret);
		} else {
			$("#userFindPwdIdRight").val(ret);
			$("#useridInputToFind").val("");
			$("#userFindPwd").modal('hide');
			$("#userInputEmail").modal('show');
		}
	});
});
//验证邮箱是否正确
$("body").on("click", "#inputEmail_findPWD", function() {
	var email = $("#useridEmail").val();
	var rere = $().checkEmailInLaw(email);
	if (rere != null) {
		$("#wrong_tips_findPWD_inputEmail").html("<i class='fa fa-remove'>" + rere);
		return;
	}
	$("#wrong_tips_findPWD_inputEmail").html("");
	$(this).css("display", "none");
	$("#waitingtips_findPWD_inputEmail").css("display", "inline");
	$.post("../LoginServlet", {
		"userId": $("#userFindPwdIdRight").val(),
		"email": email,
		"Type": "emailCheck"
	}, function(ret) {
		$("#waitingtips_findPWD_inputEmail").css("display", "none");
		if (ret.split(" ")[0] == "Success") {
			$("#wrong_tips_findPWD_inputEmail").html(""); //清空错误信息
			$("#finPWD_inputEmail_successEmail").css("display", "inline");
			$("#waitingtips_findPWD_sendEmail").css("display", "inline");
			// console.log(ret)
			var peoId = ret.split(" ")[1];
			// console.log(peoId);
			$("#peoIdRight").val(peoId);
			console.log(peoId+" "+$("#peoIdRight").val())
			$.post("../EmailServlet", {
				"peoId": peoId,
				"Type": "send"
			}, function(ret) {
				$("#finPWD_inputEmail_successEmail").css("display", "none");
				$("#waitingtips_findPWD_sendEmail").css("display", "none");
				if (ret.split(" ")[0] == "Yes") {
					$("#success-info-Register").css("display", "inline");
					$("#success-info-Register").html(ret.split(" ")[1]);
					$("#userInputEmail").modal('hide');
					$('#emailCodeModal').modal('show');
					$("#forgetSetPwd").val("true");
				} else {
					$("#error-info-Register").css("display", "inline");
					$("#error-info-Register").html(ret.split(" ")[1]);
				}

			});
		} else {
			$("#wrong_tips_findPWD_inputEmail").html(ret);
		}
	});
});

//注册验证
$("#registerJustify").click(function() {
	$("#forgetSetPwd").val("false");
	var peoId = $("#peoIdInput").val();
	var email = $("#emailInput").val();
	// alert(email);
	var rere = $().checkIdInLaw(peoId);
	if (rere != null) {
		$('#error-info-Register').html(rere);
		$("#error-info-Register").css("display", "inline");
		return;
	}
	rere = $().checkEmailInLaw(email);
	if (rere != null) {
		$('#error-info-Register').html(rere);
		$("#error-info-Register").css("display", "inline");
	}

	$("#success-info-Register").css("display", "none");
	$("#warning-info-Register").css("display", "none");
	$("#error-info-Register").css("display", "none");
	$("#registerJustify").attr("disabled", true);
	// console.log($("#registerJustify").html());
	$("#registerJustify").html("请稍候<i id='registerJustifyiCon'></i>");
	$("#registerJustifyiCon").addClass("fa fa-refresh fa-spin fa-fw");


	$.post("../LoginServlet", {
		"peoId": peoId,
		"email": email,
		"Type": "regiCheck"
	}, function(ret) {
		$("#registerJustify").attr("disabled", false);
		$("#registerJustify").html("注册");
		if (ret != null) {
			if (ret.split(" ")[0] == "Success") {
				$("#warning-info-Register").css("display", "inline");
				$("#warning-info-Register").html(ret.split(" ")[1]);
				$("#registerJustify").attr("disabled", true);

				$.post("../EmailServlet", {
					"peoId": peoId,
					"Type": "send"
				}, function(ret) {
					// alert(ret)
					$("#warning-info-Register").css("display", "none");
					// alert(typeof(ret.split(" ")[0]))
					// alert(ret.split(" ")[0])
					// alert(ret.split(" ")[0] == "Yes")
					if (ret.split(" ")[0] == "Yes") {
						$("#success-info-Register").css("display", "inline");
						$("#success-info-Register").html(ret.split(" ")[1]);
						$('#emailCodeModal').modal('show');
					} else {
						$("#error-info-Register").css("display", "inline");
						$("#error-info-Register").html(ret.split(" ")[1]);
					}
					$("#registerJustify").attr("disabled", false);
				});
			} else {
				$("#error-info-Register").css("display", "inline");
				$("#error-info-Register").html(ret);
			}

		}
		// if (ret == "null") {
		// 	$("#error-info-Register").css("display", "inline");
		// 	$("#error-info-Register").html(ret);
		// } else {
		// 	swal({
		// 		title: "登陆成功！",
		// 		text: "尊敬的" + ret + userId + "欢迎您使用银行信贷系统！\n该对话框将在两秒后自动为您跳转！",
		// 		showConfirmButton: false,
		// 		type: "success",
		// 	});

		// 	if (ret == "Customer") {
		// 		setTimeout("$(location).attr('href', 'Customer/CustomerMain.html')", 2000);
		// 	} else if (ret == "Manager") {
		// 		setTimeout("$(location).attr('href', 'Manager/ManagerMain.html')", 2000);
		// 	} else {
		// 		$("#wrong_tips").css("display", "inline");
		// 		$("#wrong_tips").html("尊敬的管理员" + peoId + "欢迎您使用银行信贷系统！");
		// 	}

		// }

	});
});

$("#signUp").click(function() {
	$('#ZhangCheng').addClass("right-panel-active");
});

$("#signIn").click(function() {
	$('#ZhangCheng').removeClass("right-panel-active");
});
